package anna.lopez.mora;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class Background extends SurfaceView {

	private SurfaceHolder holder;
	private DrawThread bgdt;
	
	//classes auxiliars
	private Nuvols nuv;
	//control thread
	private boolean surfacecreated;
	private boolean threadrunning;
	//fons
	Paint fons;
	
	public Background(Context context) {
		super(context);
		init();
	}

	public Background(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public Background(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	private void init() {
		surfacecreated = false;
		threadrunning = false;
		holder = getHolder();
		holder.addCallback( new Callback() {
			
			public void surfaceDestroyed(SurfaceHolder holder) {
				stopthread();
				surfacecreated = false;
			}
			
			public void surfaceCreated(SurfaceHolder holder) {
				holder.setFormat(PixelFormat.RGBA_8888);
				
				fons = new Paint(); 
				Shader shader = new SweepGradient(0,0,0xFFFFCC00, 0xFFFFCC33);
			    fons.setShader(shader);  
				
			    nuv = new Nuvols();
				surfacecreated = true;
			}
			
			public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
				startthread();
			}
		});  
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawRect(0,0,getWidth(),getHeight(), fons);
		nuv.actualitza();
		nuv.draw(canvas);
	}
	
	public void startthread() {
		if(!surfacecreated) return;
		if(threadrunning) return;
		bgdt = new DrawThread(this);
		bgdt.setRunning(true);
		bgdt.start();
		threadrunning = true;
	}
	
	public void stopthread() {
		if(!surfacecreated) return;
		if(!threadrunning) return;
		boolean retry = true;
		while (retry) {
			try {
				bgdt.setRunning(false);
				bgdt.join();
				threadrunning = false;
				retry = false;
				
			} catch (InterruptedException e) {}
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}
}

//package anna.lopez.mora;
//
//
//
//
//import android.content.Context;
//import android.graphics.Bitmap;
//import android.graphics.Canvas;
//import android.graphics.Paint;
//import android.graphics.PixelFormat;
//import android.graphics.Shader;
//import android.graphics.SweepGradient;
//import android.util.AttributeSet;
//import android.util.Log;
//import android.view.SurfaceHolder;
//import android.view.SurfaceHolder.Callback;
//import android.view.SurfaceView;
//
//public class Background extends SurfaceView{
//	private boolean surfacecreated = false;
//	private boolean threadrunning = false;
//	private DrawThread thread;
//	private SurfaceHolder holder;
//	private Paint fons;
//	private Bitmap nuvol;
//	private float i;
//	private float j;
//	private Nuvols nuv;
//	public Background(Context context) {
//		super(context);
//		init();
//	}
//	
//	public Background(Context context, AttributeSet attrs) {
//		super(context, attrs);
//		init();
//	}
//
//	public Background(Context context, AttributeSet attrs, int defStyle) {
//		super(context, attrs, defStyle);
//		init();
//	}
//	
//	private void init() {
//		GV.Instancies.background = this;
//		holder = getHolder();
//		holder.addCallback(new Callback() {
//			
//			public void surfaceDestroyed(SurfaceHolder holder) {
//				stopthread();
//				surfacecreated = false;
//			}
//			
//			public void surfaceCreated(SurfaceHolder holder) {
//				holder.setFormat(PixelFormat.RGBA_8888);
//				fons = new Paint();
//				nuv = new Nuvols();
//				Shader shader = new SweepGradient(0,0,0xFFFFCC00, 0xFFFFCC33);
//				//fons.setColor(0xFF00CCFF);
//				fons.setShader(shader);
//				surfacecreated = true;
//			}
//			
//			public void surfaceChanged(SurfaceHolder holder, int format, int width,
//					int height) {
//					startthread();
//			}
//		});
//			
//	}
//
//	@Override
//	protected void onDraw(Canvas canvas) {
//		canvas.drawRect(0, 0, getWidth(),getHeight(), fons);
//		nuv.actualitza();
//		nuv.draw(canvas);
//	}
//	
//	public void startthread() {
//		if(!surfacecreated) return;
//		if(threadrunning) return;
//		thread = new DrawThread(this);
//		thread.setRunning(true);
//		thread.start();
//		threadrunning = true;
//	}
//	public void stopthread() {
//		Log.w("BG THREAD","Join del thread "+surfacecreated+" "+threadrunning);
//		if(!surfacecreated) return;
//		if(!threadrunning) return;
//		boolean retry = true;
//		while (retry) {
//			try {
//				thread.setRunning(false);
//				thread.join();
//				Log.w("BG THREAD","Join del thread");
//				threadrunning = false;
//				retry = false;
//				
//			} catch (InterruptedException e) {}
//		}
//	}
//	
//	
//}
